package com.example.myblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.myblog.entity.BlogMessage;
import com.example.myblog.entity.Comment;
import com.example.myblog.exception.BaseException;
import com.example.myblog.mapper.CommentMapper;
import com.example.myblog.result.BaseCodeEnum;
import com.example.myblog.service.BlogMessageService;
import com.example.myblog.service.CommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.myblog.utils.UserThreadLocal;
import com.example.myblog.vo.CommentVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 评论管理 服务实现类
 * </p>
 *
 * @author 叶兴
 * @since 2022-09-29
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    CommentMapper commentMapper;
    BlogMessageService blogMessageService;

    @Autowired
    public void setCommentMapper(CommentMapper commentMapper) {
        this.commentMapper = commentMapper;
    }
    @Autowired
    public void setBlogMessageService(BlogMessageService blogMessageService) {
        this.blogMessageService = blogMessageService;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertOneCommentForBlog(CommentVO commentVO) throws BaseException {
        // 1. 新建一个 comment
        Comment comment = new Comment();
        // 2. 获取当前用户
        String username = UserThreadLocal.getUserDtoName();
        // 3. 为 comment 赋予属性
        comment.setUserName(username);
        BeanUtils.copyProperties(commentVO,comment);
        comment.setCommentTime(LocalDateTime.now());
        // 4. 插入评论
        int i = commentMapper.insert(comment);
        if (i != 1) {
            throw new BaseException(BaseCodeEnum.INSERT_FAILED);
        }
        // 5. 同时更新文章信息
        blogMessageService.updateBlogMessageComments(commentVO.getBlogId(), username);
        return 0;
    }

    @Override
    public List<Comment> getAllComments(String id) throws BaseException {
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("blog_id",id);
        List<Comment> list = list(wrapper);
        if (list == null || list.size() == 0) {
            throw new BaseException(BaseCodeEnum.NO_COMMENTS);
        }
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertOneCommentForUser(CommentVO commentVO) throws BaseException {
        // 1. 新建一个 comment
        Comment comment = new Comment();
        // 2. 获取当前用户
        String username = UserThreadLocal.getUserDtoName();
        // 3. 为 comment 赋予属性
        comment.setUserName(username);
        BeanUtils.copyProperties(commentVO,comment);
        comment.setCommentTime(LocalDateTime.now());
        // 4. 插入评论
        int i = commentMapper.insert(comment);
        if (i != 1) {
            throw new BaseException(BaseCodeEnum.INSERT_FAILED);
        }
        // 5. 同时更新文章信息
        blogMessageService.updateBlogMessageComments(commentVO.getBlogId(), username);
        return 0;
    }

    @Override
    public int updateCommentLikes(Integer commentId,String username) throws BaseException {
        UpdateWrapper<Comment> wrapper = new UpdateWrapper<>();
        wrapper.setSql("comment_likes = comment_likes + 1");
        wrapper.eq("user_name", username).eq("id", commentId);
        int update = commentMapper.update(null, wrapper);
        if (update != 1){
            throw new BaseException(BaseCodeEnum.UPDATE_FAILED);
        }
        return update;
    }
}
